package com.beiye.drivertransport.SubActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.beiye.drivertransport.R;
import com.beiye.drivertransport.activity.TwoBaseAty;
import com.beiye.drivertransport.bean.LoginUserBean;
import com.beiye.drivertransport.bean.UseTcHourChartBean;
import com.beiye.drivertransport.bean.UserCourseChartBean;
import com.beiye.drivertransport.chart.NewPieChart;
import com.beiye.drivertransport.chart.PieDataEntity;
import com.beiye.drivertransport.http.Login;
import com.beiye.drivertransport.utils.ChartUtilUseCourse;
import com.beiye.drivertransport.utils.ChartUtilUseTcHour;
import com.beiye.drivertransport.utils.UserManger;
import com.githang.statusbar.StatusBarCompat;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.jobs.MoveViewJob;

import java.util.ArrayList;
import java.util.List;


import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;
//报表统计
public class DataStatisticsActivity extends TwoBaseAty {


    ImageView img_back;
//    @Bind(R.id.img_data)
//    TextView img_data;

    TextView tv_title;

    NewPieChart pieChart1;

    NewPieChart pieChart2;

    NewPieChart pieChart3;

    BarChart barChart1;

    BarChart barChart2;

    RelativeLayout re_nodata;

    RelativeLayout re_data;

    RelativeLayout re_nodata1;

    RelativeLayout re_data1;

    RelativeLayout re_nodata2;

    RelativeLayout re_data2;

    RelativeLayout re_nodata3;

    RelativeLayout re_data3;

    RelativeLayout re_nodata4;

    RelativeLayout re_data4;

    TextView tv_shenhe;

    TextView tv_noshenhe;

    TextView tv_daishenhe;

    TextView tv_zhunshi;

    TextView tv_buxue;

    TextView tv_zhunshi1;

    TextView tv_buxue1;


    private int[] mColors = {0xFF28CA52,0xFFF8C345,0xFF25A2DC};
    private int[] mColors1 = {0xFF28CA52,0xFFF8C345};
//    private PopupWindow mPopWindow;
//    private PopWindowApt popWindowApt;

    @Override
    public int getLayoutId() {
        return R.layout.activity_data_statistics;
    }

    public void onClick(View view) {
        if (view.getId() == R.id.img_back) {
            finish();
            //            case R.id.tv_data:
//                if(popWindowApt==null||popWindowApt.getCount()==1)
//                {
//
//                    return;
//                }else{
//
//                    showPopupWindow();
//                }
//                break;
        }
    }

//    private void showPopupWindow() {
//        //设置contentView
//        View contentView = LayoutInflater.from(this).inflate(R.layout.popwindow_layout, null);
//        mPopWindow = new PopupWindow(contentView,
//                WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, true);
//        //设置各个控件的点击响应
//        ListView lv_company = (ListView) contentView.findViewById(R.id.lv_company);
//        lv_company.setAdapter(popWindowApt);
//        mPopWindow.showAtLocation(tv_title, Gravity.CENTER, 0, 0);
//
//        lv_company.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
//                if (popWindowApt != null) {
//
//                    String orgName = popWindowApt.getItem(position).getOrgName();
//                    String orgId = popWindowApt.getItem(position).getOrgId();
//                    mPopWindow.dismiss();
//                    tv_title.setText(orgName);
//
//                    //存储一份文件
//                    SharedPreferences sp =getSharedPreferences("orgId20",
//                            Context.MODE_PRIVATE);
//                    SharedPreferences.Editor editor = sp.edit();
//                    editor.putString("orgId", orgId);
//                    editor.commit();
//                    requestData();
//
//                }
//            }
//        });
//    }


    @Override
    public void initData() {

        img_back = (ImageView) findViewById(R.id.img_back);
        tv_title = (TextView) findViewById(R.id.tv_data);
        pieChart1 = (NewPieChart) findViewById(R.id.chart1);
        pieChart2 = (NewPieChart) findViewById(R.id.chart2);
        pieChart3 = (NewPieChart) findViewById(R.id.chart3);
        barChart1 = (BarChart) findViewById(R.id.bct_frist1);
        barChart2 = (BarChart) findViewById(R.id.bct_frist2);
        re_nodata = (RelativeLayout) findViewById(R.id.re_data);
        re_data = (RelativeLayout) findViewById(R.id.re_data1);
        re_nodata1 = (RelativeLayout) findViewById(R.id.re_data2);
        re_data1 = (RelativeLayout) findViewById(R.id.re_data3);
        re_nodata2 = (RelativeLayout) findViewById(R.id.re_data6);
        re_data2 = (RelativeLayout) findViewById(R.id.re_data7);
        re_nodata3 = (RelativeLayout) findViewById(R.id.re_data4);
        re_data3 = (RelativeLayout) findViewById(R.id.re_data5);
        re_nodata4 = (RelativeLayout) findViewById(R.id.re_data8);
        re_data4 = (RelativeLayout) findViewById(R.id.re_data9);
        tv_shenhe = (TextView) findViewById(R.id.tv_data20);
        tv_noshenhe = (TextView) findViewById(R.id.tv_data21);
        tv_daishenhe = (TextView) findViewById(R.id.tv_data22);
        tv_zhunshi = (TextView) findViewById(R.id.tv_data23);
        tv_buxue = (TextView) findViewById(R.id.tv_data24);
        tv_zhunshi1 = (TextView) findViewById(R.id.tv_data25);
        tv_buxue1 = (TextView) findViewById(R.id.tv_data26);

        StatusBarCompat.setStatusBarColor(this, Color.parseColor("#1F80C4"));
        //选中公司方法
        initCompany();
        ChartUtilUseTcHour.initBarchart(barChart1);
        ChartUtilUseCourse.initBarchart(barChart2);
    }

    private void initCompany() {

//        LoginUserBean userInfo = UserManger.getUserInfo();
//        LoginUserBean.DataBean data = userInfo.getData();
//        if(data==null)
//        {
//            return;
//        }
//        List<LoginUserBean.DataBean.CurUserOrgBean> curuserOrglist = data.getCur_userOrg();
//        if(curuserOrglist.size()==0)
//        {
//            img_data.setVisibility(View.INVISIBLE);
//            return;
//        }
//        String orgName = curuserOrglist.get(0).getOrgName();
//        String orgId = curuserOrglist.get(0).getOrgId();
        //存储一份文件
        Bundle bundle = getIntent().getExtras();
        String orgId = bundle.getString("orgId");
        String orgName = bundle.getString("orgName");
        SharedPreferences sp = getSharedPreferences("orgId20",
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("orgId", orgId);
        editor.commit();
        tv_title.setText(orgName);
        requestData();
//        popWindowApt = new PopWindowApt(this,curuserOrglist,R.layout.popwindow_item_layout);
//        if(popWindowApt.getCount()==1)
//        {
//            img_data.setVisibility(View.INVISIBLE);
//        } else{
//            img_data.setVisibility(View.VISIBLE);
//        }

    }



    @Override
    public void requestData() {
        LoginUserBean userInfo = UserManger.getUserInfo();
        LoginUserBean.DataBean data = userInfo.getData();
        if(data==null)
        {
            return;
        }
        String userId = data.getUserId();//useid
        SharedPreferences sp = getSharedPreferences("orgId20",
                Context.MODE_PRIVATE);
        String orgId = sp.getString("orgId", "");
        if(orgId.equals(""))
        {
            return;
        }
        new Login().getChatTime(userId,orgId,12,this,1);
        new Login().getChatSubTime(userId,orgId,12,this,2);

    }

    @Override
    public void onSuccess(String result, Call call, Response response, int what) {
        if(what==1)
        {
        UseTcHourChartBean useTcHourChartBean = JSON.parseObject(result, UseTcHourChartBean.class);
        UseTcHourChartBean.DataBean data = useTcHourChartBean.getData();
        int notCheckNo = data.getNotCheckNo();//待审核数
        int notPassCheckNo = data.getNotPassCheckNo();//未通过审核数
        int passCheckNo = data.getPassCheckNo();//通过审核数
        if(notCheckNo==0&&notPassCheckNo==0&&passCheckNo==0)
        {
            re_nodata.setVisibility(View.VISIBLE);
            re_data.setVisibility(View.GONE);
        }else {
            re_nodata.setVisibility(View.GONE);
            re_data.setVisibility(View.VISIBLE);
            List<PieDataEntity> dataEntities = new ArrayList<>();
            PieDataEntity entity1 = new PieDataEntity("通过审核",passCheckNo,mColors[0]);
            PieDataEntity entity2 = new PieDataEntity("未通过审核",notPassCheckNo,mColors[1]);
            PieDataEntity entity3 = new PieDataEntity("待审核",notCheckNo,mColors[2]);
            dataEntities.add(entity1);
            dataEntities.add(entity2);
            dataEntities.add(entity3);
            tv_shenhe.setText(passCheckNo+"学时");
            tv_noshenhe.setText(notPassCheckNo+"学时");
            tv_daishenhe.setText(notCheckNo+"学时");
            pieChart1.setDataList(dataEntities);
            pieChart1.startAnimation(2000);


        }
        int muNo = data.getMuNo();
        int notMuNo = data.getNotMuNo();
        if(muNo==0&&notMuNo==0)
        {
            re_nodata1.setVisibility(View.VISIBLE);
            re_data1.setVisibility(View.GONE);
        }else {
            re_nodata1.setVisibility(View.GONE);
            re_data1.setVisibility(View.VISIBLE);
            List<PieDataEntity> dataEntities = new ArrayList<>();
            PieDataEntity entity1 = new PieDataEntity("准时学习",notMuNo,mColors1[0]);
            PieDataEntity entity2 = new PieDataEntity("补学",muNo,mColors1[1]);
            dataEntities.add(entity1);
            dataEntities.add(entity2);
            tv_zhunshi.setText(notMuNo+"学时");
            tv_buxue.setText(muNo+"学时");
            pieChart2.setDataList(dataEntities);
            pieChart2.startAnimation(2000);
        }

        List<UseTcHourChartBean.DataBean.StatListBean> statList = data.getStatList();
        if(statList.size()==0)
        {
            re_nodata2.setVisibility(View.VISIBLE);
            re_data2.setVisibility(View.GONE);
        }else{
            re_nodata2.setVisibility(View.GONE);
            re_data2.setVisibility(View.VISIBLE);
            ChartUtilUseTcHour.initCheckData(barChart1,statList);
        }
        }else if(what==2)
        {
            UserCourseChartBean userCourseChartBean = JSON.parseObject(result, UserCourseChartBean.class);
            UserCourseChartBean.DataBean data = userCourseChartBean.getData();
            int muNo = data.getMuNo();//补学数
            int notMuNo = data.getNotMuNo();//准时学习
            if(muNo==0&&notMuNo==0)
            {
                re_nodata3.setVisibility(View.VISIBLE);
                re_data3.setVisibility(View.GONE);
            }else {
                re_nodata3.setVisibility(View.GONE);
                re_data3.setVisibility(View.VISIBLE);
                List<PieDataEntity> dataEntities = new ArrayList<>();
                PieDataEntity entity1 = new PieDataEntity("准时学习",notMuNo,mColors1[0]);
                PieDataEntity entity2 = new PieDataEntity("补学",muNo,mColors1[1]);
                dataEntities.add(entity1);
                dataEntities.add(entity2);
                tv_zhunshi1.setText(notMuNo+"课程");
                tv_buxue1.setText(muNo+"课程");
                pieChart3.setDataList(dataEntities);
                pieChart3.startAnimation(2000);
            }
            List<UserCourseChartBean.DataBean.StatListBean> statList = data.getStatList();
            if(statList.size()==0)
            {
                re_nodata4.setVisibility(View.VISIBLE);
                re_data4.setVisibility(View.GONE);
            }else{
                re_nodata4.setVisibility(View.GONE);
                re_data4.setVisibility(View.VISIBLE);
                ChartUtilUseCourse.initCheckData(barChart2,statList);
            }

        }
        super.onSuccess(result, call, response, what);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        //MoveViewJob.destroy();
    }
}
